#----------------------------------------------------------------------------
# Setup the project
cmake_minimum_required(VERSION 3.16...3.27)
if(${CMAKE_VERSION} VERSION_LESS 3.12)
    cmake_policy(VERSION ${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION})
endif()

project(g4test LANGUAGES CXX)
include(00config.cmake)

set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
set(CMAKE_CXX_STANDARD 17)

#000000000000000000000000000000000000 检查是否在 conan2 中构建
# message(DEBUG "[conan2] ${CMAKE_TOOLCHAIN_FILE} = ${CMAKE_TOOLCHAIN_FILE}")
set(IN_CONAN_BUILD FALSE)
if(EXISTS "${CMAKE_TOOLCHAIN_FILE}")
    get_filename_component(TOOLCHAIN_NAME "${CMAKE_TOOLCHAIN_FILE}" NAME)
    if(TOOLCHAIN_NAME STREQUAL "conan_toolchain.cmake")
        message(STATUS ">>> Running under Conan 2 environment")
        set(IN_CONAN_BUILD TRUE)
    endif()
endif()

#=========================================== 添加 G4 搜索路径,
if(WIN32)
    add_compile_definitions(WIN32)
    add_compile_options(/utf-8)
    list(PREPEND CMAKE_PREFIX_PATH "C:/cpplibs/geant4-11.3.2/lib/cmake/Geant4")
elseif(LINUX)
    if(CMAKE_BUILD_TYPE STREQUAL "Debug") # geant4库 debug版路径
        list(PREPEND CMAKE_PREFIX_PATH "$ENV{HOME}/cpplibs/geant4v11.3.2debug")
    else() # geant4库 release 版路径
        list(PREPEND CMAKE_PREFIX_PATH "$ENV{HOME}/cpplibs/geant4v11.3.2")
    endif()
endif(WIN32)

set(myEXE ${PROJECT_NAME})

#=========================================== conan2 兼容设置
# conan2 的 expat 包的 target 是小写的 expat::expat;
# geant4 期待大写的 EXPAT::EXPAT
# if(IN_CONAN_BUILD)
find_package(expat REQUIRED)
add_library(EXPAT::EXPAT ALIAS expat::expat)
# endif()

find_package(fmt REQUIRED)
# add_subdirectory(3rdlibs/fmt-12.1.0)
find_package(spdlog CONFIG REQUIRED)

#----------------------------------------------------------------------------
# Find Geant4 package, activating all available UI and Vis drivers by default
# You can set WITH_GEANT4_UIVIS to OFF via the command line or ccmake/cmake-gui
# to build a batch mode only executable
#
option(WITH_GEANT4_UIVIS "Build example with Geant4 UI and Vis drivers" ON)
if(WITH_GEANT4_UIVIS)
    find_package(Geant4 REQUIRED ui_all vis_all)
else()
    find_package(Geant4 REQUIRED)
endif()
message(DEBUG "[EXPAT] Geant4_builtin_expat_FOUND = ${Geant4_builtin_expat_FOUND}")
message(DEBUG "[EXPAT] ${Geant4_LIBRARIES} = ${${Geant4_LIBRARIES}}")


#----------------------------------------------------------------------------
# Setup Geant4 include directories and compile definitions
#
include(${Geant4_USE_FILE})

#----------------------------------------------------------------------------
# Locate sources and headers for this project
#
include_directories(${PROJECT_SOURCE_DIR}/include
    ${Geant4_INCLUDE_DIR})

##00000000000000000000000000000000000000000000 头文件列表
set(headers
    include/ActionInitialization.hh
    include/args.hxx
    include/CliArgs.hh
    include/DetectorConstruction.hh
    include/DetectorMessenger.hh
    include/LETdMessenger.hh
    include/MyConstant.hh
    include/PhysicsList.hh
    include/PhysicsListMessenger.hh
    include/PrimaryGeneratorAction.hh
    include/PrimaryGeneratorMessenger.hh
    include/PSSDoseAveragedLET.hh
    include/RunAction.hh
    include/RunData.hh
    include/Util.hh
    include/MyMacros.hh
    include/MyInitial.hh
)
##00000000000000000000000000000000000000000000 源文件列表
set(sources
    src/ActionInitialization.cc
    src/DetectorConstruction.cc
    src/DetectorMessenger.cc
    src/LETdMessenger.cc
    src/PhysicsList.cc
    src/PhysicsListMessenger.cc
    src/PrimaryGeneratorAction.cc
    src/PrimaryGeneratorMessenger.cc
    src/PSSDoseAveragedLET.cc
    src/RunAction.cc
    src/RunData.cc
)


##----------------------------------------------------------------------------
## Add the executable, and link it to the Geant4 libraries
add_executable(${myEXE} g4test.cc ${sources} ${headers})

target_link_libraries(${myEXE}
    ${Geant4_LIBRARIES}
    fmt::fmt
    spdlog::spdlog $<$<BOOL:${MINGW}>:ws2_32>
)

my_config_msvc_relwithdebinfo(${myEXE})
## my_cp_dlls(${myEXE})

##----------------------------------------------------------------------------
## Install the executable to 'bin' directory under CMAKE_INSTALL_PREFIX
install(TARGETS ${myEXE} DESTINATION bin)


##----------------------------------------------------------------------------
## 将所有 scripts 拷贝到 build 目录. exe 运行的时候，会从所在目录寻找 scripts.
set(EXAMPLEB1_SCRIPTS
    init_vis.mac
    vis.mac
    letd.mac
    qa1.mac
)

foreach(_script ${EXAMPLEB1_SCRIPTS})
    configure_file(
        ${PROJECT_SOURCE_DIR}/${_script}
        ${PROJECT_BINARY_DIR}/${_script}
        COPYONLY
    )
endforeach()
